package order

import (
	"context"
	"errors"
	"parking-lot-go-push/model"
	"parking-lot-go-push/model/park"
	"parking-lot-go-push/mongodb"
	"time"

	"go.mongodb.org/mongo-driver/bson"
)

type OrderStruct struct {
	Id             int       `gorm:"type:int(11) auto_increment; NOT NULL; primary_key"`
	StaticParkCode string    `gorm:"type:varchar(20); COMMENT:'静态停车场编号'"`
	ParkCode       string    `gorm:"type:varchar(20); COMMENT:'停车场编号'"`
	CarNumber      string    `gorm:"type:varchar(20); COMMENT:'车牌号'"`
	CarNoColor     int       `gorm:"type:tinyint(1); COMMENT:'车牌颜色 1-蓝牌 3-黄牌 6-绿牌 7-白牌 8-黑牌'"`
	CarType        int       `gorm:"type:tinyint(1); COMMENT:'车型 1: 小型车 2: 中型车 3: 大型车 4: 其 他'"`
	OrderNo        string    `gorm:"type:varchar(255); COMMENT:'系统单号'"`
	Status         int       `gorm:"type:tinyint(1); COMMENT:'订单状态 0 预约中, 1在场地内  2已结束'"`
	OrderType      int       `gorm:"type:tinyint(1); COMMENT:'1 小强停车'"`
	CreatedAt      time.Time `gorm:"type:datetime"`
	UpdatedAt      time.Time `gorm:"type:datetime"`
}

func (c *OrderStruct) TableName() string {
	return "order"
}

func (c *OrderStruct) OverStop(parkCode string, orderType int, orderNo string) (int, error) {

	var err error
	var count64 int64
	var configStopNum int
	var nowStopNum int

	var orderData OrderStruct
	err = model.Db.Table(orderData.TableName()).
		Where("status", 1).
		Where("static_park_code", parkCode).
		Where("order_type", orderType).
		Count(&count64).Error

	if err != nil {
		return 0, errors.New("停车数量查询失败," + err.Error())
	}

	//停车数量
	nowStopNum = int(count64)

	// 定义一个变量来存储具体停车场信息的结构体
	switch orderType {
	case 1: //小强
		var parkingLotXiaoQiangData park.ParkingLotXiaoQiangStruct
		parkingLotData := &parkingLotXiaoQiangData
		err = model.Db.Table(parkingLotXiaoQiangData.TableName()).Where("park_code", parkCode).First(parkingLotData).Error
		if err != nil {

			//进场剔除
			entryErrorPark(orderNo)
			//出场剔除
			outErrorPark(orderNo)

			return 0, errors.New("停车场配置信息查询失败," + err.Error())
		}

		//配置停车数量
		configStopNum = parkingLotData.Number
	case 2: //泊安飞
		var parkingLotParknflyData park.ParkingLotParknflyStruct
		parkingLotData := &parkingLotParknflyData
		err := model.Db.Table(parkingLotParknflyData.TableName()).Where("park_code", parkCode).First(parkingLotData).Error
		if err != nil {

			//进场剔除
			entryErrorPark(orderNo)
			//出场剔除
			outErrorPark(orderNo)

			return 0, errors.New("停车场配置信息查询失败," + err.Error())
		}

		//配置停车数量
		configStopNum = parkingLotData.Number
	default:
		// 其他类型的处理
		return 0, errors.New("订单类型不存在")
	}

	//超停状态
	abnormal := 0

	var orderExcess OrderExcess
	orderExcess = OrderExcess{
		ParkCode:  parkCode,
		OrderType: orderType,
	}

	//超出车位数量
	if nowStopNum > configStopNum {
		//调用超停方法
		orderExcess.NormalNumber = configStopNum
		orderExcess.OutNumber = nowStopNum - configStopNum
		orderExcess.Excess()

		abnormal = 1
	} else {
		//停止所有超停记录
		orderExcess.StopExcess()
	}

	return abnormal, nil
}

// 获取订单
func (c *OrderStruct) FetchOrder(orderNo string, status, orderType int) (OrderStruct, error) {

	var orderData OrderStruct

	err := model.Db.Table(c.TableName()).
		Where("order_no", orderNo).
		Where("status", status).
		Where("order_type", orderType).
		First(&orderData).
		Error

	if err != nil {
		return orderData, err
	}

	return orderData, nil
}

// 进错停车场
func entryErrorPark(orderNo string) {
	ctx := context.TODO()
	var carsEntryMongoDb mongodb.CarsEntryMongoDbStruct
	model.MongoDb.Collection(carsEntryMongoDb.MongoDBTableName()).DeleteMany(ctx, bson.M{"order_no": orderNo})
}

// 出错停车场
func outErrorPark(orderNo string) {
	ctx := context.TODO()
	var carsOutMongoDb mongodb.CarsOutMongoDbStruct
	model.MongoDb.Collection(carsOutMongoDb.MongoDBTableName()).DeleteMany(ctx, bson.M{"order_no": orderNo})
}
